package Simulation;

import java.util.Vector;

import mathwork.Mathwork;
import fxana.FxRawDataSet;
import fxana.RawDataCell;

public class ConditionLSMDLC extends ConditionLSM {

	public ConditionLSMDLC() {
	}

	public ConditionLSMDLC(FxRawDataSet ds, RawDataCell today,
			EvaluatorParaFlag flag) {
		super(ds, today, flag);
	}

	@Override
	protected Condition GetNew(FxRawDataSet ds, RawDataCell today,
			EvaluatorParaFlag flag) {
		return new ConditionLSMDLC(ds,today,flag);
	}
	
	@Override
	protected void UpdateLossCutValue(FxRawDataSet ds, RawDataCell today,
			EvaluatorParaFlag flag) {
		Vector<RawDataCell> RawDataBuf=ds.GetRawDataBuffer();
		Vector<Double> hs=new Vector<Double>();
		Vector<Double> sl=new Vector<Double>();
		
		int loop;
		int count;
		int numberofday=500;
		int index=ds.GetIndexByDate(today.GetRecDate());
		if(index-1<numberofday){
			numberofday=index-1;
		}
		
		for(loop=index-1,count=0;count<numberofday;loop--,count++){
			hs.add(RawDataBuf.elementAt(loop).GetHighStart());
			sl.add(RawDataBuf.elementAt(loop).GetStartLow());
		}
		
		Double losscuth=today.GetStartValue()+Mathwork.lognormalest(hs, 0.98).elementAt(0);
		Double losscutl=today.GetStartValue()-Mathwork.lognormalest(sl, 0.98).elementAt(0);
		
		if(this.State==ConditionState.WAITBUY){
			if(losscuth.compareTo(this.LossCutValue)<=0){
				this.LossCutValue=losscuth;
			}
		}else if (this.State==ConditionState.WAITSELL){
			if(losscutl.compareTo(this.LossCutValue)>=0){
				this.LossCutValue=losscutl;
			}
		}
		return;
	}
}
